package com.changgou.goods.controller;
import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.pojo.Goods;
import com.changgou.goods.service.SpuService;
import com.changgou.goods.pojo.Spu;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@CrossOrigin
@RequestMapping("/spu")
public class SpuController {


    @Autowired
    private SpuService spuService;

    /**
     * 查询全部数据
     * @return
     */
    @GetMapping
    public Result<Goods> findAll(){
        //默认查询前十条数据
        //List<Spu> spuList = spuService.findAll();
        Map<String, Object> searchMap = new HashMap<String, Object>(8);
        searchMap.put("status","1");
        PageResult<Goods> pageResult = spuService.findPage(searchMap,1, 10);
        return new Result<Goods>(true, StatusCode.OK,"查询成功",pageResult);
    }

    /***
     * 页面加载时,查询未审核的前十条商品
     * @return
     */
    @GetMapping("/unaudited")
    public Result<Goods> findUnauditedGoods(){
        //默认查询前十条数据
        Map<String, Object> searchMap = new HashMap<String, Object>(8);
        searchMap.put("status","0");
        searchMap.put("isDelete","0");
        PageResult<Goods> pageResult = spuService.findPage(searchMap,1, 10);
        return new Result<Goods>(true, StatusCode.OK,"查询成功",pageResult);

    }

    /***
     * 回收站页面加载时,查询已逻辑删除前十条商品
     * @return
     */
    @GetMapping("/deleted")
    public Result<Goods> findDeletedGoods(){
        //默认查询前十条数据
        Map<String, Object> searchMap = new HashMap<String, Object>(8);
        searchMap.put("isDelete","1");
        PageResult<Goods> pageResult = spuService.findPage(searchMap,1, 10);
        return new Result<Goods>(true, StatusCode.OK,"查询成功",pageResult);
    }


    /***
     * 根据ID查询数据
     * @param id 商品spuId
     * @return Result<Spu>
     */
    @GetMapping("/findSpuById/{id}")
    public Result<Spu> findSpuById(@PathVariable String id){
        Spu spu = spuService.findById(id);
        return new Result<Spu>(true,StatusCode.OK,"查询成功",spu);
    }

    /***
     * 根据ID查询商品数据
     * @param spuId 商品spuId
     * @return Result<Goods>
     */
    @GetMapping("/{spuId}")
    public Result<Goods> findGoodsById(@PathVariable("spuId") String spuId){
        Goods goods = spuService.findGoodsById(spuId);
        return new Result<Goods>(true,StatusCode.OK,"查询成功",goods);
    }


    /***
     * 新增数据
     * @param spu
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Spu spu){
        spuService.add(spu);
        return new Result(true,StatusCode.OK,"添加成功");
    }


    /***
     * 修改数据
     * @param spu
     * @param id
     * @return
     */
    @PutMapping(value="/{id}")
    public Result update(@RequestBody Spu spu,@PathVariable String id){
        spu.setId(id);
        spuService.update(spu);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /***
     * 修改商品数据
     * @param goods 包含spu以及skuList的goods
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Goods goods){
        spuService.update(goods);
        return new Result(true,StatusCode.OK,"修改成功");
    }


    /***
     * 根据ID删除品牌数据(逻辑删除)
     * @param spuId
     * @return
     */
    @DeleteMapping(value = "/{spuId}" )
    public Result delete(@PathVariable("spuId") String spuId){
        spuService.delete(spuId);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 多条件搜索品牌数据
     * @param searchMap
     * @return
     */
    @GetMapping(value = "/search" )
    public Result<Goods> findList(@RequestParam Map<String,Object> searchMap){
        List<Goods> goodsList = spuService.findList(searchMap);
        return new Result<Goods>(true,StatusCode.OK,"查询成功",goodsList);
    }


    /***
     * 分页搜索实现
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<Goods> findPage(@RequestParam Map<String,Object> searchMap, @PathVariable  int page, @PathVariable  int size){
        searchMap.put("status","1");
        PageResult<Goods> pageResult = spuService.findPage(searchMap, page, size);
        return new Result<Goods>(true,StatusCode.OK,"查询成功",pageResult);
    }

    /***
     * 分页搜索未审核商品
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/search/unaudited/{page}/{size}" )
    public Result<Goods> findUnauditedGoodsByPage(@RequestParam Map<String,Object> searchMap, @PathVariable  int page, @PathVariable  int size){
        searchMap.put("status","0");
        searchMap.put("isDelete","0");
        PageResult<Goods> pageResult = spuService.findPage(searchMap, page, size);
        return new Result<Goods>(true,StatusCode.OK,"查询成功",pageResult);
    }

    /***
     * 分页搜索已删除的商品(回收站显示商品)
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/search/deleted/{page}/{size}" )
    public Result<Goods> findDeletedGoodsByPage(@RequestParam Map<String,Object> searchMap, @PathVariable  int page, @PathVariable  int size){
        searchMap.put("isDelete","1");
        PageResult<Goods> pageResult = spuService.findPage(searchMap, page, size);
        return new Result<Goods>(true,StatusCode.OK,"查询成功",pageResult);
    }

    /***
     * 商品上架
     * @param spuId 商品spuId
     * @return
     */
    @PutMapping("/push/{spuId}")
    public Result pushSpu(@PathVariable("spuId")String spuId){
        spuService.pushSpu(spuId);
        return new Result(true,StatusCode.OK,"商品上架成功");
    }

    /***
     * 商品下架
     * @param spuId 商品spuId
     * @return
     */
    @PutMapping("/pull/{spuId}")
    public Result pullSpu(@PathVariable("spuId")String spuId){
        spuService.pullSpu(spuId);
        return new Result(true,StatusCode.OK,"商品下架成功");
    }

    /***
     * 根据ID删除品牌数据(完全删除,从回收站删除)
     * @param spuId 商品id
     * @return
     */
    @DeleteMapping("/realDel/{spuId}")
    public Result realDel(@PathVariable("spuId")String spuId){
        spuService.realDel(spuId);
        return new Result(true,StatusCode.OK,"商品彻底删除成功");
    }

    /**
     * 从回收站还原逻辑删除的商品
     * @param spuId  为spuId
     * @return
     */
    @PutMapping("/restore/{spuId}")
    public Result restore(@PathVariable("spuId")String spuId){
        spuService.restore(spuId);
        return new Result(true,StatusCode.OK,"商品还原成功");
    }


}